<template>
  <div class="xtx-city">
    <!-- 这个 active 控制了背景颜色 -->
    <div class="select" @click="toggleDialog" :class="{ active: active }">
      <!-- 没有地址，就显示提示信息 -->
      <span class="placeholder" v-if="!resultAddress">请选择配送地址</span>
      <!-- 有的话，就显示具体的拼接后的地址 -->
      <span class="value" v-else>{{ resultAddress }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <!-- 这个 active 控制了下拉框的显示/隐藏 -->
    <div class="option" v-if="active">
      <span
        class="ellipsis"
        v-for="i in cityDataList"
        :key="i"
        @click="optionClick(i)"
        >{{ i.name }}</span
      >
    </div>
  </div>
</template>
<script>
import { reactive, ref, toRefs, watch } from 'vue'
import { requestCity } from '@/utils/request'
export default {
  name: 'XtxCity',
  setup () {
    // 点击显示/隐藏下拉框
    const active = ref(false)
    const show = () => {
      active.value = true
    }
    const hide = () => {
      active.value = false
    }
    const toggleDialog = () => {
      // active.value = !active.value
      active.value ? hide() : show()
    }
    const cityDataList = ref([])
    // 获取城市数据
    const loadCityData = async () => {
      const res = await requestCity()
      cityDataList.value = res.data
      // 缓存完整的城市信息到 cacheCityList 中
      cacheCityList.value = res.data
    }
    loadCityData()

    // #1 点击出现当前下面的市或区，把当前的 areaList 给 cityDataList
    // #2 准备变量，存储每次选择的信息，方便后续使用
    // #3 准备一个变量，缓存完成的城市信息，为了下次再点击的时候，也能重现
    // #4 监听 active 的变化，如果 active 是 true，就把 cacheCityList 给 cityDataList
    const cacheCityList = ref([])
    const changeResult = reactive({
      provinceCode: '', // 省code
      provinceName: '', // 省名称
      cityCode: '', // 城市code
      cityName: '', // 城市名称
      countyCode: '', // 地区code
      countyName: '', // 地区名
      resultAddress: '' // 最终结果地址
    })
    const optionClick = (item) => {
      if (item.level === 0) {
        changeResult.provinceCode = item.code
        changeResult.provinceName = item.name
      }
      if (item.level === 1) {
        changeResult.cityCode = item.code
        changeResult.cityName = item.name
      }
      // 当前点击的区
      if (item.level === 2) {
        changeResult.countyCode = item.code
        changeResult.countyName = item.name
        changeResult.resultAddress =
          changeResult.provinceName +
          '-' +
          changeResult.cityName +
          '-' +
          changeResult.countyName
        // 最后一层，隐藏弹框
        hide()
      }
      cityDataList.value = item.areaList
    }

    watch(active, (newValue) => {
      if (newValue) {
        cityDataList.value = cacheCityList.value
        // 重新选择的时候，恢复默认的数据
        changeResult.provinceCode = ''
        changeResult.provinceName = ''
        changeResult.cityCode = ''
        changeResult.cityName = ''
        changeResult.countyCode = ''
        changeResult.countyName = ''
        changeResult.resultAddress = ''
      }
    })

    return {
      active,
      toggleDialog,
      cityDataList,
      optionClick,
      ...toRefs(changeResult)
    }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 10px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
